package by.pavlyukevich.paymentsystem.dataaccess.payment.impl;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.SetJoin;

import org.springframework.stereotype.Repository;

import by.pavlyukevich.paymentsystem.dataaccess.impl.AbstractDaoImpl;
import by.pavlyukevich.paymentsystem.dataaccess.payment.PaymentDetailDao;
import by.pavlyukevich.paymentsystem.datamodel.erip.EripName;
import by.pavlyukevich.paymentsystem.datamodel.erip.EripName_;
import by.pavlyukevich.paymentsystem.datamodel.payment.PaymentDetail;

@Repository
public class PaymentDetailDaoImpl extends AbstractDaoImpl<Integer, PaymentDetail> implements
		PaymentDetailDao {

	protected PaymentDetailDaoImpl() {
		super(PaymentDetail.class);
	}

	@Override
	public List<PaymentDetail> getPaymentsByErip(EripName eripName) {
		CriteriaBuilder cBuilder = getEntityManager().getCriteriaBuilder();

		CriteriaQuery<PaymentDetail> root = cBuilder.createQuery(PaymentDetail.class);
		Root<EripName> criteria = root.from(EripName.class);

		SetJoin<EripName, PaymentDetail> joinPaymentDetail = criteria
				.join(EripName_.paymentDetails);

		root.select(joinPaymentDetail).where(
				cBuilder.equal(criteria.get(EripName_.id), eripName.getId()));

		TypedQuery<PaymentDetail> query = getEntityManager().createQuery(root);
		return query.getResultList();

	}
}
